package com.seantone.inappupdate.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.seantone.inappupdate.core.define.CheckUpdateParams;
import com.seantone.inappupdate.core.define.CheckUpdateRet;

public class RootActivity  extends AppCompatActivity {
    protected CheckUpdateRet mCheckUpdateRet = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String infoJsonStr = getIntent().getStringExtra("info");
        mCheckUpdateRet = infoJsonStr.isEmpty() ? null: new Gson().fromJson(infoJsonStr, CheckUpdateRet.class);
//        downloadInfo = getIntent().getParcelableExtra("info");
//        AppUpdateUtils.getInstance().addAppDownloadListener(appDownloadListener);
//        AppUpdateUtils.getInstance().addAppUpdateInfoListener(appUpdateInfoListener);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String infoJsonStr = getIntent().getStringExtra("info");
        mCheckUpdateRet = infoJsonStr.isEmpty() ? null: new Gson().fromJson(infoJsonStr, CheckUpdateRet.class);

//        AppUpdateUtils.getInstance().addAppDownloadListener(appDownloadListener);
//        AppUpdateUtils.getInstance().addAppUpdateInfoListener(appUpdateInfoListener);
    }

    @Override
    public void onBackPressed() {
        if (mCheckUpdateRet != null) {
//            if (!mCheckUpdateRet.isForceUpdateFlag()) {
//                super.onBackPressed();
//            }
        } else{
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        AppUpdateUtils.getInstance().clearAllListener();
    }

    /**
     * 启动Activity
     *
     * @param context
     * @param info
     */
    public static void launchActivity(Context context, CheckUpdateRet info, Class cla) {
        Intent intent = new Intent(context, cla);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP + Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("info", new Gson().toJson(info,CheckUpdateRet.class ));
        context.startActivity(intent);
    }
}
